using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using WebTempGen;

namespace Classifier
{
    class NaiveBayes
    {
        protected List<ColorScheme> prevElementsList;
        protected ColorScheme newElement;
        private float totalPages = 0;

        public NaiveBayes()
        {
            prevElementsList = new List<ColorScheme>();
            LoadPreviousIntoMemory();

            newElement = new ColorScheme();
        }

        public NaiveBayes(ColorScheme we)
        {
            prevElementsList = new List<ColorScheme>();
            LoadPreviousIntoMemory();

            newElement = we;
        }

        public void setWebElement(ColorScheme we)
        {
            newElement = we;
        }

        public void DetermineProbabilities()
        {
            bool found = false;
            foreach (ColorScheme prevElements in prevElementsList)
            {
                if (prevElements.Equals(this.newElement))
                {
                    found = true;
                    totalPages += 1;

                    prevElements.Number_of_times += 1;
                    prevElements.Probability = prevElements.Number_of_times / totalPages * 100;
                }
                else
                {
                    prevElements.Probability = prevElements.Number_of_times / (totalPages + 1) * 100;
                }

            }

            if (!found)
            {
                totalPages += 1;

                this.newElement.Number_of_times = 1;
                this.newElement.Probability = this.newElement.Number_of_times / totalPages * 100;
                prevElementsList.Add(this.newElement);
            }

            WriteToDatabase();
        }

        private void WriteToDatabase()
        {
            OleDbConnection conn = new OleDbConnection(Constants.CONNSTR);
            try
            {
                conn.Open();

                String sql = "";

                foreach (ColorScheme we in prevElementsList)
                {
                    if (we.Id >= 0)
                    {
                        sql = "UPDATE PROB SET PROBABILITY = " + we.Probability.ToString() +
                              ", NUMBER_OF_TIMES = " + we.Number_of_times.ToString() +
                              " WHERE ID = " + we.Id.ToString();
                    }
                    else
                    {
                        
                        sql = "INSERT INTO PROB (BACKGROUND,FONT,ACTIVELINK,HOVERLINK,LINK,VISITEDLINK,NUMBER_OF_TIMES,PROBABILITY)" +
                              "VALUES('" + ColorTranslator.ToHtml(we.Background) + "','" + ColorTranslator.ToHtml(we.Fonts[0]) + "','" + ColorTranslator.ToHtml(we.Links[ColorScheme.LinkType.Active]) + "','" +
                              ColorTranslator.ToHtml(we.Links[ColorScheme.LinkType.Hover]) + "','" + ColorTranslator.ToHtml(we.Links[ColorScheme.LinkType.Link]) + "','" + ColorTranslator.ToHtml(we.Links[ColorScheme.LinkType.Visited])
                              + "'," + we.Number_of_times + "," +
                              we.Probability + ");";
                    }

                    OleDbCommand comm = new OleDbCommand(sql, conn);
                    comm.ExecuteNonQuery();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("DOUG");
            }
            finally
            {
                conn.Close();
            }
        }

        private void LoadPreviousIntoMemory()
        {
            try
            {
                DataTable dbElements = new DataTable("prev");

                OleDbConnection conn = new OleDbConnection(Constants.CONNSTR);
                conn.Open();

                OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from Prob", Constants.CONNSTR);
                adapter.Fill(dbElements);

                conn.Close();
                
                ColorScheme elem;

                foreach (DataRow row in dbElements.Rows)
                {
                    elem = new ColorScheme();
                    elem.Id = Int32.Parse(row["ID"].ToString());
                    elem.Links.Add(ColorScheme.LinkType.Active, ColorTranslator.FromHtml(row["ACTIVELINK"].ToString()));
                    elem.Links.Add(ColorScheme.LinkType.Hover, ColorTranslator.FromHtml(row["HOVERLINK"].ToString()));
                    elem.Links.Add(ColorScheme.LinkType.Link, ColorTranslator.FromHtml(row["LINK"].ToString()));
                    elem.Links.Add(ColorScheme.LinkType.Visited, ColorTranslator.FromHtml(row["VISITEDLINK"].ToString()));
                    elem.Background = ColorTranslator.FromHtml(row["BACKGROUND"].ToString());
                    elem.Fonts[0] = ColorTranslator.FromHtml(row["FONT"].ToString());
                    elem.Number_of_times = float.Parse(row["NUMBER_OF_TIMES"].ToString());
                    elem.Probability = float.Parse(row["PROBABILITY"].ToString());

                    totalPages += elem.Number_of_times;

                    prevElementsList.Add(elem);
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.InnerException.StackTrace);
            }
        }

        public void OutputResultsData()
        {
            OleDbConnection conn = new OleDbConnection(Constants.CONNSTR);
            conn.Open();

            OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from Prob", Constants.CONNSTR);
            DataTable dbElements = new DataTable("prev");
            adapter.Fill(dbElements);

            conn.Close();
        }
    }
}
